
from insects_detection.pic_utils import pic_tools, batch_manager, txt_tools
from insects_detection.insects_utils import insects_tools
import os

from insects_detection.project_utils import project_tools
from utils.redis_utils import RedisTool

import shutil


# 建立与阿里云服务器redis的连接，并获取操作实例
# r=redis.StrictRedis(host='39.102.44.159', port=6379, db=0, password='inetlabredis', decode_responses=True)
r = RedisTool(host='43.137.16.144', port=6379, db=0)
# 准备工作文件夹
project_root_dir = project_tools.project_root_path() + os.sep + 'insects_detection'
project_tools.prepare_working_dirs(project_root_dir)
# # 设定文件读取路径
# gallery_path = "/home/lizhenghao/PycharmProjects/insects_detection_flask_backend/insects_detection/insects_imgs/insects_data/June"
#                # "/home/lizhenghao/insects_data/insects_images/June"
gallery_path = project_root_dir+os.sep+project_tools.insects_img_dirname+os.sep+"tmzx"
# # gallery_path='D:\\1Learning\江苏大学\虫害识别图像数据\第二次病虫害数据图像（03.03）'
model_path = project_root_dir + os.sep + project_tools.models_dirname + os.sep + project_tools.model_name
# single_img_path = project_root_dir + os.sep + project_tools.single_img_processing_dirname
# # 测试代码
# pic_tools.rename_imgs(gallery_path, '1', '.jpg')
processed_gallery_path = project_root_dir + os.sep + project_tools.processed_img_dirname
# # 图像预处理（降低分辨率、抠图、重命名）
store_folder_path = pic_tools.write_all_processed_imgs(gallery_path, processed_gallery_path)

processed_imgs_path = project_root_dir + os.sep + project_tools.processed_img_dirname+os.sep+"tmzx"
chas_data_path = project_root_dir + os.sep + project_tools.characteristic_dirname
# # 获取所有图片上昆虫的特征矩阵
pic_tools.extract_all_imgs_characteristcs(processed_imgs_path,chas_data_path)

insects_chas_path =project_root_dir+os.sep + project_tools.characteristic_dirname+os.sep+"tmzx"
# # 获取温室大棚病虫害分类计数信息
# scaler_path=r'E:\code\NjjProject\NJJ_Algorithm_identification\BingCH\insects_detection_flask_backend2\insects_detection\\trainning_models\\Model4May_scaler.pkl'
scaler_path=project_root_dir + os.sep + project_tools.models_dirname + os.sep + 'Model4May_scaler.pkl'
# 自动清空文件夹
def delete_folder_contents(folder_path):
    try:
        # 删除文件夹中的文件
        for filename in os.listdir(folder_path):
            file_path = os.path.join(folder_path, filename)
            if os.path.isfile(file_path):
                os.remove(file_path)
            elif os.path.isdir(file_path):
                shutil.rmtree(file_path)  # 递归删除子文件夹和文件
        print(f"成功删除 {folder_path} 中的所有内容。")
    except Exception as e:
        print(f"删除 {folder_path} 内容时出错：{str(e)}")


def detailedInsectsInfo(file_path: str):
    # # 获取所有图片上昆虫的特征矩阵
    reprocess_path = file_path.replace('insects_imgs', 'processed_imgs')
    pic_tools.write_all_processed_imgs(file_path, reprocess_path)


    txt_file_path = file_path.replace('insects_imgs', 'chas_data')
    pic_tools.extract_all_imgs_characteristcs(reprocess_path, txt_file_path)

    detailed_insects_info = insects_tools.classify_green_house_insects(txt_file_path, model_path, scaler_path)

    reprocess_path += (".." + os.sep)
    txt_file_path += (".." + os.sep)
    file_path += (".." + os.sep)
    
    delete_folder_contents(reprocess_path)
    delete_folder_contents(txt_file_path)
    delete_folder_contents(file_path)
    return detailed_insects_info